package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.WebPage;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 显示的网页的Activity
 * 1.显示用户协议
 * 2.显示网页端下载界面
 * 3.显示一些常用网页界面
 * 4.帮助界面
 * 5.认证协议界面
 * 6.兼职发布协议界面
 * 7.关于兼职家招兼职版
 * 8.兼职猫求职端下载
 */
public class WebPageActivity extends BaseActivity {


    public static final int WEBPAGE_REGISTER = 1;//兼职家招兼职版用户注册协议
    public static final int WEBPAGE_PUBLISH_HELP = 2;//兼职家招兼职版发布帮助
    public static final int WEBPAGE_PUBLISH_PROTOCAL = 3;//兼职家招兼职版发布兼职协议
    public static final int WEBPAGE_HELP = 4;//兼职家招兼职版帮助中心
    public static final int WEBPAGE_ABOUT_US = 5;//兼职家，关于我们
    public static final int WEBPAGE_DOWNLOAD = 6;//网页宣传下载网页
    public static final int WEBPAGE_IDNTIFICATION = 7;//兼职家招兼职版认证协议
    public static final int WEBPAGE_LETTER = 8;

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;


    private WebView webView;
    private ProgressDialog progressDialog;
    private int webPageType = -1;
    private WebPage webPage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_page);
        ButterKnife.inject(this);
        webView = (WebView) findViewById(R.id.wbv_web_page);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在加载");
        webPageType = getIntent().getIntExtra("WebPageType", -1);
        if(webPageType == -1){
            ToastUtils.showToast(getApplicationContext(),"数据出错");
            finish();
        }
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//        WebPageLoadNetTask webPageLoadNetTask = new WebPageLoadNetTask();
//        webPageLoadNetTask.execute();
        String url = null;
        String title = null;
        if(webPageType == -1){
            ToastUtils.showToast(this,"出错");
            finish();
        }
        switch(webPageType){

            case WEBPAGE_REGISTER:
                url = "http://parttime.duapp.com/html/z_register_agreement.html";
                title = "用户注册协议";
                break;
            case WEBPAGE_PUBLISH_HELP:
                url = "http://parttime.duapp.com/html/z_using_agreement.html";
                title = "发布说明";
                break;
            case WEBPAGE_PUBLISH_PROTOCAL:
                url = "http://parttime.duapp.com/html/z_using_agreement.html";
                title = "发布协议";
                break;
            case WEBPAGE_HELP:
                url = "http://parttime.duapp.com/html/z_helper_center.html";
                title = "帮助中心";
                break;
            case WEBPAGE_ABOUT_US:
                url = "http://u.eqxiu.com/s/p4N0Pqpg?eqrcode=1&from=singlemessage&isappinstalled=0";
                title = "关于我们";
                break;
            case WEBPAGE_DOWNLOAD:
                url = "https://www.baidu.com/";
                title = "下载";
                break;
            case WEBPAGE_IDNTIFICATION:
                url = "http://parttime.duapp.com/html/z_certification_agreement.html";
                title = "认证协议";
                break;
            case WEBPAGE_LETTER:
                url = "https://www.baidu.com/";
                title = "公函";
                break;

        }
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        tvTitle.setText(title);
//

    }


    class WebPageLoadNetTask extends AsyncTask<Void, Void, String> {


        public WebPageLoadNetTask() {
            super();
        }

        @Override
        protected String doInBackground(Void... params) {

            AVQuery avObjectAVQuery = new AVQuery("WebPage");
            avObjectAVQuery.whereEqualTo("type",webPageType);
            avObjectAVQuery.orderByDescending("type");
            try {

                List list = avObjectAVQuery.find();
                AVObject avObject = (AVObject) list.get(0);
                webPage = new WebPage(avObject);
                return null;
            } catch (AVException e) {
                e.printStackTrace();
                return ExceptionUtils.getExceptionInfo(e);
            }
        }

        @Override
        protected void onPostExecute(String s) {
            if (s == null) {
                System.out.println(webPage.getUrl());
                webView.loadUrl(webPage.getUrl());
                webView.setWebViewClient(new WebViewClient() {
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        view.loadUrl(url);
                        return true;
                    }
                });
                tvTitle.setText(webPage.getTitle());
            }else{
                ToastUtils.showToast(getApplicationContext(),s);
            }
            progressDialog.dismiss();
            super.onPostExecute(s);
        }

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            super.onPreExecute();
        }
    }


}
